#include "jx_uart_send.h"

// 串口通信消息头
const unsigned char g_uart_send_head[] = {
  0xaa, 0x55
};

// 串口通信消息尾
const unsigned char g_uart_send_foot[] = {
  0x55, 0xaa
};

// 串口发送函数实现
void _uart_send_impl(unsigned char* buff, int len) {
  // TODO: 调用项目实际的串口发送函数
  /*
  int i = 0;
  unsigned char c;
  for (i = 0; i < len; i++) {
    c = buff[i];
    printf("%02X ", c);
  }
  printf("\n");
  */
}

// action: ready
void _uart_ready(int ready_in) {
  uart_param_t param;
  int i = 0;
  unsigned char buff[UART_SEND_MAX] = {0};
  for (i = 0; i < UART_MSG_HEAD_LEN; i++) {
      buff[i + 0] = g_uart_send_head[i];
  }
  buff[2] = U_MSG_ready;
  param.d_int = ready_in;
  _int16_to_int32(&param);
  buff[3] = param.d_ucs[0];
  buff[4] = param.d_ucs[1];
  buff[5] = param.d_ucs[2];
  buff[6] = param.d_ucs[3];
  for (i = 0; i < UART_MSG_FOOT_LEN; i++) {
      buff[i + 7] = g_uart_send_foot[i];
  }
  _uart_send_impl(buff, 9);
}

// action: newData
void _uart_newData(double hum_in, double tem_in, int lig_in, int soil_in) {
  uart_param_t param;
  int i = 0;
  unsigned char buff[UART_SEND_MAX] = {0};
  for (i = 0; i < UART_MSG_HEAD_LEN; i++) {
      buff[i + 0] = g_uart_send_head[i];
  }
  buff[2] = U_MSG_newData;
  param.d_double = hum_in;
  _float_to_double(&param);
  buff[3] = param.d_ucs[0];
  buff[4] = param.d_ucs[1];
  buff[5] = param.d_ucs[2];
  buff[6] = param.d_ucs[3];
  buff[7] = param.d_ucs[4];
  buff[8] = param.d_ucs[5];
  buff[9] = param.d_ucs[6];
  buff[10] = param.d_ucs[7];
  param.d_double = tem_in;
  _float_to_double(&param);
  buff[11] = param.d_ucs[0];
  buff[12] = param.d_ucs[1];
  buff[13] = param.d_ucs[2];
  buff[14] = param.d_ucs[3];
  buff[15] = param.d_ucs[4];
  buff[16] = param.d_ucs[5];
  buff[17] = param.d_ucs[6];
  buff[18] = param.d_ucs[7];
  param.d_int = lig_in;
  _int16_to_int32(&param);
  buff[19] = param.d_ucs[0];
  buff[20] = param.d_ucs[1];
  buff[21] = param.d_ucs[2];
  buff[22] = param.d_ucs[3];
  param.d_int = soil_in;
  _int16_to_int32(&param);
  buff[23] = param.d_ucs[0];
  buff[24] = param.d_ucs[1];
  buff[25] = param.d_ucs[2];
  buff[26] = param.d_ucs[3];
  for (i = 0; i < UART_MSG_FOOT_LEN; i++) {
      buff[i + 27] = g_uart_send_foot[i];
  }
  _uart_send_impl(buff, 29);
}

